#coding=utf-8
#Boa:Dialog:DlgRecordEditor

import wx
import wx.lib.buttons

from product import CDataRecord


def create(parent):
    return DlgRecordEditor(parent)

from product import CDataRecord

[wxID_DLGRECORDEDITOR, wxID_DLGRECORDEDITORBTNCANCEL, 
 wxID_DLGRECORDEDITORBTNEDIT, wxID_DLGRECORDEDITORBTNOK, 
 wxID_DLGRECORDEDITORSTATICTEXT1, wxID_DLGRECORDEDITORTCCONTENT, 
 wxID_DLGRECORDEDITORTCONTENT, wxID_DLGRECORDEDITORTCOPINION, 
 wxID_DLGRECORDEDITORTCTITLE, wxID_DLGRECORDEDITORTCTYPE, 
 wxID_DLGRECORDEDITORTOPINION, wxID_DLGRECORDEDITORTTYPE, 
] = [wx.NewId() for _init_ctrls in range(12)]

class DlgRecordEditor(wx.Dialog):
    def _init_ctrls(self, prnt):
        # generated method, don't edit
        wx.Dialog.__init__(self, id=wxID_DLGRECORDEDITOR,
              name=u'DlgRecordEditor', parent=prnt, pos=wx.Point(323, 197),
              size=wx.Size(714, 505), style=wx.DEFAULT_DIALOG_STYLE,
              title=u'\u4e8b\u9879\u7f16\u8f91')
        self.SetClientSize(wx.Size(706, 471))
        self.SetFont(wx.Font(12, wx.SWISS, wx.NORMAL, wx.BOLD, False,
              u'\u6977\u4f53_GB2312'))
        self.Center(wx.BOTH)

        self.staticText1 = wx.StaticText(id=wxID_DLGRECORDEDITORSTATICTEXT1,
              label=u'\u6807\u9898\uff1a', name='staticText1', parent=self,
              pos=wx.Point(40, 24), size=wx.Size(56, 14), style=0)

        self.tcTitle = wx.TextCtrl(id=wxID_DLGRECORDEDITORTCTITLE,
              name=u'tcTitle', parent=self, pos=wx.Point(96, 16),
              size=wx.Size(408, 26), style=0, value=u'')

        self.tType = wx.StaticText(id=wxID_DLGRECORDEDITORTTYPE,
              label=u'\u7c7b\u578b\uff1a', name=u'tType', parent=self,
              pos=wx.Point(40, 64), size=wx.Size(51, 16), style=0)

        self.tcType = wx.TextCtrl(id=wxID_DLGRECORDEDITORTCTYPE, name=u'tcType',
              parent=self, pos=wx.Point(96, 56), size=wx.Size(184, 26), style=0,
              value=u'')

        self.tContent = wx.StaticText(id=wxID_DLGRECORDEDITORTCONTENT,
              label=u'\u5185\u5bb9\uff1a', name=u'tContent', parent=self,
              pos=wx.Point(40, 104), size=wx.Size(51, 16), style=0)

        self.tcContent = wx.TextCtrl(id=wxID_DLGRECORDEDITORTCCONTENT,
              name=u'tcContent', parent=self, pos=wx.Point(96, 104),
              size=wx.Size(536, 120), style=wx.TE_MULTILINE, value=u'')

        self.tOpinion = wx.StaticText(id=wxID_DLGRECORDEDITORTOPINION,
              label=u'\u610f\u89c1\uff1a', name=u'tOpinion', parent=self,
              pos=wx.Point(40, 248), size=wx.Size(51, 16), style=0)

        self.tcOpinion = wx.TextCtrl(id=wxID_DLGRECORDEDITORTCOPINION,
              name=u'tcOpinion', parent=self, pos=wx.Point(96, 248),
              size=wx.Size(536, 112), style=wx.TE_MULTILINE, value=u'')

        self.btnOK = wx.Button(id=wxID_DLGRECORDEDITORBTNOK,
              label=u'\u786e\u8ba4', name=u'btnOK', parent=self,
              pos=wx.Point(160, 408), size=wx.Size(93, 26), style=0)
        self.btnOK.Bind(wx.EVT_BUTTON, self.OnBtnOKButton,
              id=wxID_DLGRECORDEDITORBTNOK)

        self.btnCancel = wx.Button(id=wxID_DLGRECORDEDITORBTNCANCEL,
              label=u'\u53d6\u6d88', name=u'btnCancel', parent=self,
              pos=wx.Point(360, 408), size=wx.Size(93, 26), style=0)
        self.btnCancel.Bind(wx.EVT_BUTTON, self.OnBtnCancelButton,
              id=wxID_DLGRECORDEDITORBTNCANCEL)

        self.btnEdit = wx.lib.buttons.GenToggleButton(id=wxID_DLGRECORDEDITORBTNEDIT,
              label=u'\u7f16\u8f91\u6a21\u5f0f', name=u'btnEdit', parent=self,
              pos=wx.Point(536, 16), size=wx.Size(96, 28), style=0)
        self.btnEdit.SetToggle(True)
        self.btnEdit.Bind(wx.EVT_BUTTON, self.OnBtnEditButton,
              id=wxID_DLGRECORDEDITORBTNEDIT)

    def __init__(self, parent):
        self._init_ctrls(parent)

        self.m_record = None
        
    def setData(self, obj, edit=True):
        self.m_record = obj
        self.tcTitle.SetValue(obj.getValue(obj.fi_title))
        self.tcType.SetValue(obj.getValue(obj.fi_type))
        self.tcContent.SetValue(obj.getValue(obj.fi_content))
        self.tcOpinion.SetValue(obj.getValue(obj.fi_feedback))
        
        self.setState(edit)
        
    def setState(self, edit=True):
        label = (edit and u'编辑模式') or u'浏览模式'
        self.btnEdit.SetLabel(label)
        
        self.tcTitle.Enable(edit)
        self.tcType.Enable(edit)
        self.tcContent.Enable(edit)
        self.tcOpinion.Enable(edit)
        
    def OnBtnOKButton(self, event):
        data = {CDataRecord.fi_title:self.tcTitle.GetValue(),
                CDataRecord.fi_type:self.tcType.GetValue(),
                CDataRecord.fi_content:self.tcContent.GetValue(),
                CDataRecord.fi_feedback:self.tcOpinion.GetValue()}
        
        if (self.m_record is not None):
            self.m_record.setMulFieldVal(data)
            self.m_record.update()
        else:
            obj = CDataRecord.factory()
            obj.setMulFieldVal(data)
            obj.insert()
            self.m_record = obj
        
        self.EndModal(wx.ID_OK)

    def OnBtnCancelButton(self, event):
        self.EndModal(wx.ID_CANCEL)

    def OnBtnEditButton(self, event):
        self.setState(event.GetIsDown())

            
